package com.example.shixi.service;

import com.example.shixi.domain.BlogArticleType;
import com.example.shixi.domain.BlogArticleTypeExample;
import com.example.shixi.domain.BlogArticleType;
import com.example.shixi.domain.BlogArticleTypeExample;
import com.example.shixi.mapper.BlogArticleTypeMapper;
import com.example.shixi.pojo.UserPage;
import com.example.shixi.util.JSONResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleTypeService {
    private Date date = new Date();
    @Resource
    private BlogArticleTypeMapper blogArticleTypeMapper;
    public JSONResult query(BlogArticleType bat, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        BlogArticleTypeExample blogArticleTypeExample = new BlogArticleTypeExample();
        BlogArticleTypeExample.Criteria criteria = blogArticleTypeExample.createCriteria();
        //BlogArticleTypeExample blogArticleTypeExampleAll = new BlogArticleTypeExample();
        UserPage<BlogArticleType> userPage= new UserPage<>();

        //List<BlogArticleType> blogArticleTypesAll = blogArticleTypeMapper.selectByExample(blogArticleTypeExampleAll);
        if(!ObjectUtils.isEmpty(bat)){
            criteria.andTypeNameLike("%"+bat.getTypeName()+"%");
        }

        List<BlogArticleType> blogArticleTypes = blogArticleTypeMapper.selectByExample(blogArticleTypeExample);
        PageInfo<BlogArticleType> pageInfo= new PageInfo<>(blogArticleTypes);
        userPage.setList(blogArticleTypes);
        userPage.setTotal(pageInfo.getTotal());
        userPage.setPageNum(pageNum);
        userPage.setPageSize(pageSize);
        return JSONResult.success(userPage);

    }
    public BlogArticleType selectByName(String name){
        BlogArticleTypeExample blogArticleTypeExample1 = new BlogArticleTypeExample();
        BlogArticleTypeExample.Criteria criteria = blogArticleTypeExample1.createCriteria();
        criteria.andTypeNameEqualTo(name);
        List<BlogArticleType> list = blogArticleTypeMapper.selectByExample(blogArticleTypeExample1);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }else{
            return list.get(0);
        }

    }
    public JSONResult add(BlogArticleType bat) {
        BlogArticleType blogArticleType = selectByName(bat.getTypeName());
        if(ObjectUtils.isEmpty(blogArticleType)){

            bat.setUpdateTime(date);
            blogArticleTypeMapper.insert(bat);
            return JSONResult.success("添加成功");
        }else{
            return JSONResult.error("该文章类型已存在");
        }

    }

    public JSONResult del(Long id) {
        blogArticleTypeMapper.deleteByPrimaryKey(id);
        return JSONResult.success("成功");
    }

    public JSONResult update(BlogArticleType bat) {
        bat.setUpdateTime(date);
        blogArticleTypeMapper.updateByPrimaryKeySelective(bat);
        return JSONResult.success("成功");
    }
}
